package com.website.dayouziblog.controller;

import com.website.dayouziblog.model.DO.ArticleDO;
import com.website.dayouziblog.model.DO.UserDO;
import com.website.dayouziblog.model.DTO.ArticleDTO;
import com.website.dayouziblog.model.entity.PageBean;
import com.website.dayouziblog.response.BaseResponse;
import com.website.dayouziblog.response.RespGenerator;
import com.website.dayouziblog.service.ArticleService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章功能Controller
 */
@Api(tags = "文章管理")
@RestController
public class ArticleController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ArticleController.class);

    @Autowired
    private ArticleService articleService;

    /**
     * 添加文章
     * @param articleDTO
     * @return
     */
    @ApiOperation(value = "添加文章")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "articleDO", value = "文章实体类", required = true, paramType = "body"),
//    })
    @PostMapping(value = "/addArticle")
    public BaseResponse addArticle(@RequestBody ArticleDTO articleDTO){

        boolean result = false;
        try {
            result = articleService.addArticle(articleDTO);
        } catch (Exception e) {
            LOGGER.debug(e.getMessage());
            e.printStackTrace();
        }
        if (result){
            return RespGenerator.returnOK("发布成功");
        }else {
            return RespGenerator.returnError("发布失败");
        }

    }

    @ApiOperation(value = "删除文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章id")
    })
    @PostMapping("/deleteArticle")
    public BaseResponse deleteArticle(@RequestParam Integer id){
        boolean result = false;
        try {
            result = articleService.deleteArticle(id);
        } catch (Exception e) {
            LOGGER.debug(e.getMessage());
            e.printStackTrace();
        }
        if (result){
            return RespGenerator.returnOK("删除成功");
        }else {
            return RespGenerator.returnError("删除失败");
        }
    }

    @ApiOperation(value = "修改文章")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "articleDO", value = "文章实体类", required = true, paramType = "body"),
//    })
    @PostMapping("/updateArticle")
    public BaseResponse updateArticle( @RequestBody ArticleDTO articleDTO){
        boolean result = false;
        try {
            result = articleService.updateArticle(articleDTO);
        } catch (Exception e) {
            LOGGER.debug(e.getMessage());
            e.printStackTrace();
        }
        if (result){
            return RespGenerator.returnOK("修改成功");
        }else {
            return RespGenerator.returnError("修改失败");
        }
    }

    @ApiOperation("查询所有文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = false, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页展示条数", required = false, defaultValue = "10")
    })
    @GetMapping("/findAllArticle")
    public BaseResponse findAllArticle(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        PageBean allArticle = null;
        try {
            allArticle = articleService.findAllArticle(page, pageSize);
        } catch (Exception e) {
            LOGGER.debug(e.getMessage());
            e.printStackTrace();
        }
        return RespGenerator.returnOK(allArticle);
    }

    @GetMapping("/testA")
    public void test(){
        System.out.println(Thread.currentThread().getName() + "before");
        articleService.testAsync();
        System.out.println(Thread.currentThread().getName() + "after");
    }

}
